﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SMS_Server;
using SMS_Server_Manager;

namespace SMS_Server_Manager.Forms
{
    public partial class ToTCP : Form
    {
        public ToTCP()
        {
            InitializeComponent();
        }

        private Action_Forward2TCP toTcp;

        public Action_Forward2TCP To_TCP
        {
            get { return toTcp; }
            set
            {
                toTcp = value;
                FunctionName.Text = toTcp.ActionName;
                ParamFormat.Text = toTcp.ActionFormat;
                IP.Text = toTcp.IP;
                Port.Text = toTcp.Port.ToString();
                hasResponse.Checked = toTcp.HasResponse;
            }
        }

        private Form Sender;

        public System.Windows.Forms.DialogResult ViewDialog(Form sender)
        {
            Sender = sender;
            return this.ShowDialog(sender);
        }

        private void CancleButton_Click(object sender, EventArgs e)
        {
            DialogResult = System.Windows.Forms.DialogResult.Cancel;
        }

        private void ContinueButton_Click(object sender, EventArgs e)
        {
            if (!validators.IsValidIP(IP.Text))
            {
                MessageBox.Show("A valid Ip is required", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            if (!validators.IsValidInt(Port.Text))
            {
                MessageBox.Show("A valid port number is required", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            if (int.Parse(Port.Text) < 1 || int.Parse(Port.Text) > 65535)
            {
                MessageBox.Show("A valid port number is required", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            //lets save the staticResponse now
            toTcp.IP = IP.Text;
            toTcp.Port = int.Parse(Port.Text);
            toTcp.HasResponse = hasResponse.Checked;
            DataAccess DA = new DataAccess(Global.GetConfigurationDatabaseName(), Global.DatabasePassword);
            if (toTcp.actionID == 0)
            {
                DA.CommandString = "INSERT INTO Action_ToTCP (FunctionName, Format, IP, PORT, HasResponse) VALUES (@FunctionName, @Format, @IP, @Port, @HasResponse);";
                DA.AddParameter("@FunctionName", toTcp.ActionName);
                DA.AddParameter("@Format", toTcp.ActionFormat);
                DA.AddParameter("@IP", toTcp.IP);
                DA.AddParameter("@Port", toTcp.Port);
                DA.AddParameter("@HasResponse", toTcp.HasResponse);
                DA.ExecuteNonQuery();
                DA.CommandString = "SELECT ActionID FROM Action_ToTCP WHERE FunctionName=@FunctionName AND Format=@Format AND IP=@IP AND Port=@Port AND HasResponse=@HasResponse;";
                DA.ExecuteReader();
                while (DA.Reader.Read())
                {
                    int ActionID = (int)DA.Reader.GetInt64(0);
                    bool ActionExist = false;
                    foreach (Incoming_Action ia in ((Function)((Mainform)Sender).FunctionList.SelectedItem).Actions)
                    {
                        if (ia.ActionID() == (long)ActionID && ia.ActionType() == ActionTypes.StaticResponse)
                        {
                            ActionExist = true;
                        }
                    }
                    if (!ActionExist)
                    {
                        toTcp.actionID = ActionID;
                        ((Function)((Mainform)Sender).FunctionList.SelectedItem).Actions.Add(toTcp);
                        break;
                    }
                }
                DA.Reader.Close();
                DA.Close();
                ((Mainform)Sender).ActionList.Items.Add(toTcp);
            }
            else
            {
                DA.CommandString = "UPDATE Action_ToHTTP SET IP=@IP, Port=@Port, HasResponse=@HasResponse WHERE ActionID=@ActionID;";
                DA.AddParameter("@IP", toTcp.IP);
                DA.AddParameter("@Port", toTcp.Port);
                DA.AddParameter("@HasResponse", toTcp.HasResponse);
                DA.AddParameter("@ActionID", toTcp.actionID);
                DA.ExecuteNonQuery();
                DA.Close();
            }
            Global.unsaved_changes = true;
            DialogResult = System.Windows.Forms.DialogResult.OK;
        }

    }
}
